<!--# 
layout("/layout/default.html",{"menu":["struts","branch_admin"],"breadcrumbs":[{"name":"机构列表",url:"branch/list"},{"name":(obj.data.branch==null?"添加机构":"编辑机构")}]}){ 
 #-->
<div class="space-4"></div>
<div class="row">
	<div class="col-md-8 col-md-offset-2 col-sm-12">
		<div class="panel panel-info">
			<div class="panel-heading">
				<h3 class="panel-title">${obj.data.branch==null ? "添加机构":"编辑机构"}</h3>
			</div>
			<div class="panel-body">
				<form id="branchForm" class="cmxform form-horizontal adminex-form" novalidate="novalidate">
					<div class="form-group ">
						<label class="control-label col-lg-3"> 上级机构 : </label>
						<div class="col-lg-9">
							<div class="input-group" onclick="showBranchSelector()">
								<input class="form-control input-mask-date" type="text" id="parentName" disabled="disabled" placeholder="不选默认为顶级机构" value="${obj.data.parent.name!}"> <span
									class="input-group-btn">
									<button class="btn btn-sm btn-primary" type="button">
										<i class="ace-icon fa fa-plus bigger-110"></i> 选择
									</button>
								</span> <input type="hidden" id="parentId" name="parentId" value="${obj.data.branch.parentId!}">
							</div>
						</div>
					</div>
					<div class="form-group ">
						<label for="用户名" class="control-label col-lg-3">机构名称 : </label>
						<div class="col-lg-9">
							<input class="form-control" type="text" placeholder="请输入机构名称" name="name" data-type='required' data-error='请输入机构名称' value="${obj.data.branch.name!}" />
						</div>
					</div>
					<div class="form-group ">
						<label class="control-label col-lg-3"> 机构编码 : </label>
						<div class="col-lg-9">
							<input class="form-control" type="text" placeholder="请输入机构编码" name="code" data-type='required' data-error='请输入机构编码' value="${obj.data.branch.code!}">
						</div>
					</div>
					<div class="form-group ">
						<label class="control-label col-lg-3"> 机构地址 : </label>
						<div class="col-lg-9">
							<div class="input-group" onclick="showMap()">
								<input class="form-control" id="address" name="address" type="text" readonly="readonly" placeholder="请选择机构地址" data-type='required' data-error='请选择机构地址'
									value="${obj.data.branch.address!}"> <span class="input-group-btn">
									<button class="btn btn-sm btn-primary" type="button">
										<i class="ace-icon fa fa-map-marker bigger-110"></i>
									</button>
								</span> <input type="hidden" id="province" name="province" value="${obj.data.branch.province!}"> <input type="hidden" id="city" name="city"
									value="${obj.data.branch.city!}"> <input type="hidden" id="district" name="district" value="${obj.data.branch.district!}"> <input type="hidden"
									id="longitude" name="longitude" value="${obj.data.branch.longitude!}"> <input type="hidden" id="latitude" name="latitude" value="${obj.data.branch.latitude!}">
							</div>
						</div>
					</div>
					<div class="form-group ">
						<label class="control-label col-lg-3"> 机构电话 : </label>
						<div class="col-lg-9">
							<input class="form-control" type="tel" placeholder="请输入机构电话" name="phone" data-type='phone' data-error='请输入正确的电话号码' value="${obj.data.branch.phone!}">
						</div>
					</div>
					<div class="form-group ">
						<label class="control-label col-lg-3"> 机构负责人 : </label>
						<div class="col-lg-9">
							<select class="form-control" name="principalId">
								<option value="0">请选择机构负责人</option>
								<!--# for(user in obj.data.users){  #-->
								<option value="${user.id!}" ${user.id == obj.data.branch.principalId! ? 'selected' : ''}>${user.realName!}</option>
								<!--# } #-->
							</select>
						</div>
					</div>
					<input type="hidden" id="id" name="id" value="${obj.data.branch.id!}">
					<div class="form-group ">
						<label class="control-label col-lg-3"> 机构描述 : </label>
						<div class="col-lg-9">
							<textarea rows="25" class="form-control" name="content">${obj.data.branch.description!}</textarea>
							<input type="hidden" name="description" id="description">
						</div>
					</div>
					<div class="pull-right" style="margin-right: 45px">
						<button type="button" class="btn btn-sm btn-primary" onclick="save(this)">
							<i class="fa fa-save"></i> 提交
						</button>
						<button class="btn btn-sm btn-default btn-back">
							<i class="fa fa-undo"></i> 返回
						</button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	var editor;
	$(function() {
		//kindeditor 初始化
		KindEditor.ready(function(K) {
			editor = K.create('textarea[name="content"]', {
				items : [ 'source', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent',
						'subscript', 'superscript', '|', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough',
						'lineheight', 'removeformat', '|', 'fullscreen', '/', 'undo', 'redo', '|', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'template', 'code',
						'clearhtml', 'quickformat', 'selectall', '|', 'image', 'multiimage', 'flash', 'media', 'insertfile', 'table', 'hr', 'baidumap', 'pagebreak', 'link',
						'unlink', '|', 'preview', 'print' ],
				uploadJson : getRootPath() + '/kind/upload'
			});
		})
	});

	function showMap() {
		baiduMap(function(result) {
			$('#address').val(result.address);
			$('#province').val(result.addressComponents.province);
			$('#city').val(result.addressComponents.city);
			$('#district').val(result.addressComponents.district);
			$('#longitude').val(result.point.lng);
			$('#latitude').val(result.point.lat);
		})
	}

	function showBranchSelector() {
		branchSelector(function(result) {
			$('#parentId').val(result.id);
			$('#parentName').val(result.name);
		})
	}

	function save(btn) {
		$('#description').val(editor.html());
		if ($('#branchForm').find('input').validation()) {//验证表单
			if ($('#id').val()) {
				$.post(getRootPath() + '/branch/edit', $('#branchForm').serialize(), function(result) {
					if (result.operationState == 'SUCCESS') {
						location.href = getRootPath() + "/branch/list";
					} else {
						validationFail(result.data.reason, btn);
					}
				}, 'json');
			} else {
				$.post(getRootPath() + '/branch/add', $('#branchForm').serialize(), function(result) {
					if (result.operationState == 'SUCCESS') {
						location.href = getRootPath() + "/branch/list";
					} else {
						validationFail(result.data.reason, btn);
					}
				}, 'json');
			}
		}
	}
</script>
<!--# } #-->